package www.openteam.com.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import www.openteam.com.R;
import www.openteam.com.bean.ProducePro;

import static www.openteam.com.tool.SplitXML.getMaxLength;
import static www.openteam.com.tool.SplitXML.rightAlign;
import static www.openteam.com.tool.SplitXML.splitStringString;

/**
 * Created by kylin on 2016/8/19.
 */
public class ProAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 产品进度适配器
     */
    private static final String TAG = "ProAdapter";
    private LayoutInflater mLayoutInflater;
    private SortedList<ProducePro.DataEntity> mSortedList;
    private Context context;
    private StringBuffer sb = new StringBuffer();
    private Resources resources;

    public ProAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(ProducePro.DataEntity.class, new SortedList.Callback<ProducePro.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(ProducePro.DataEntity o1, ProducePro.DataEntity o2) {

                if (Integer.parseInt(o1.getRownumber()) < Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber()) > Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(ProducePro.DataEntity oldItem, ProducePro.DataEntity newItem) {
                return oldItem.getRownumber().equals(oldItem.getRownumber());
            }

            @Override
            public boolean areItemsTheSame(ProducePro.DataEntity item1, ProducePro.DataEntity item2) {
                boolean statue;
                if (item1.getRownumber() == item2.getRownumber() &&
                        item1.getPdname().equals(item2.getPdname()) &&
                        item1.getRdsno().equals(item2.getRdsno()) &&
                        item1.getCustName().equals(item2.getCustName())) {
                    statue = true;
                } else {
                    statue = false;
                }
                return statue;
            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<ProducePro.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for (ProducePro.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();
    }

    public void addContext(Context context) {
        this.context = context;
    }

    public void deleteItems(ArrayList<ProducePro.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (ProducePro.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new ProAdapter.pViewHolder(mLayoutInflater.inflate(R.layout.item_base_no_audit, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        Log.d(TAG, "DataAdapter:4");
        ProducePro.DataEntity item = mSortedList.get(position);
        pViewHolder viewHolder = (pViewHolder) holder;
        viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.red_shallow));

        Log.d(TAG, "DataAdapter:4");
        resources = context.getResources();
        viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.red_shallow));
        viewHolder.textView.setText(resources.getString(R.string.idName) + item.getRownumber() + "");
//
//        "id": "<id>1</id><id>2</id><id>3</id><id>4</id><id>5</id><id>6</id><id>7</id>",
//                "partID": "<PartID></PartID><PartID></PartID><PartID></PartID><PartID></PartID><PartID></PartID><PartID></PartID><PartID></PartID>",
//                "procno": "<procno>1100</procno><procno>1300</procno><procno>3200</procno><procno>2100</procno><procno>3405</procno><procno>4930</procno><procno>5000</procno>",
//                "procname": "<name>CTP</name><name>刀模 - 舊</name><name>過油</name><name>印刷</name><name>啤面</name><name>車花</name><name>賀卡部</name>",
//                "reqnumb": "<reqnumb>1</reqnumb><reqnumb>1</reqnumb><reqnumb>1200</reqnumb><reqnumb>1200</reqnumb><reqnumb>950</reqnumb><reqnumb>800</reqnumb><reqnumb>800</reqnumb>",
//                "fnumb": null,
//                "partNo": "<PartNo>0</PartNo><PartNo>0</PartNo><PartNo>0</PartNo><PartNo>0</PartNo><PartNo>0</PartNo><PartNo>0</PartNo><PartNo>0</PartNo>",
//                "rowID": "<r

        String[] id = splitStringString(item.getRownumber(), "id");
        String[] procno = splitStringString(item.getProcno(), "procno");
        String[] pName = splitStringString(item.getProcname(), "name");
        String[] PartID = splitStringString(item.getPartid(), "PartID");
        String[] reqnumb = splitStringString(item.getReqnumb(), "reqnumb");
        String[] groupName = splitStringString(item.getGroupName(), "groupName");
        String[] partNo = splitStringString(item.getPartNo(), "PartNo");
//        if (null!=item.getPartNo()){
//        }else {
//            String[] partNo = null;
//        }
        String[] fNum = splitStringString(item.getFNumb(), "fNumb");
        double iMax = getMaxLength(id, resources.getString(R.string.idName));
        double pMax = getMaxLength(PartID, resources.getString(R.string.part_name));
        double fMax = getMaxLength(fNum, resources.getString(R.string.finish_num));
        double rMax = getMaxLength(reqnumb, resources.getString(R.string.s_num));
        double gMax = getMaxLength(groupName, resources.getString(R.string.fitting));
        for (int i = 0; i < id.length; i++) {
//       sb.append(":" + oa.getProdno()).append("\n").append("产品名称:" + oa.getProdname()).append("\n")
//                    .append("价格:" + oa.getPaynumb() + " * " + oa.getPrice() + " = ").append(oa.getPaynumb()
// * oa.getPrice()).append("\n");
            if (i == 0) {

                sb.append(resources.getString(R.string.idName) + "\t\t"
                        + resources.getString(R.string.part_name) + "\t\t"
                        + resources.getString(R.string.fitting) + "\t\t"
                        + resources.getString(R.string.finish_num) + "\t\t"
                        + resources.getString(R.string.s_num) + "\t\t" +
                        resources.getString(R.string.produce_name) + "\n");
            }
            sb.append(id[i] + rightAlign(iMax, id[i])
                    + PartID[i] + rightAlign(pMax, PartID[i]) +
                    groupName[i] + rightAlign(gMax, groupName[i]) +
                    fNum[i] + rightAlign(fMax, fNum[i])
                    + reqnumb[i] + rightAlign(rMax, reqnumb[i])
                    + pName[i] + "\n");

        }
        viewHolder.tv_title.setText(resources.getString(R.string.custom_rdsno) + ":"
                + item.getRdsno() +
                "\n" + resources.getString(R.string.cust_name) + ":" + item.getCustName() +
                "\n" + resources.getString(R.string.product_name) + ":" + item.getPdname() + "\n"
                + resources.getString(R.string.schedule) + ":" + "\n" + sb.toString()
        );

        sb.setLength(0);
        sb.delete(0, sb.length());
    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class pViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView tv_title;
        private CardView cardView;

        public pViewHolder(View itemView) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            textView = (TextView) itemView.findViewById(R.id.tv_id);
            cardView = (CardView) itemView.findViewById(R.id.card);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);

        }
    }

}